<?php
/**
 * 真题管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */

namespace app\modules\exercise\controllers;


use app\modules\cn\models\ExerciseList;
use app\modules\cn\models\ExerciseListDetail;
use app\modules\exercise\models\Report;
use app\modules\user\models\User;
use yii;
use yii\data\Pagination;
use app\libs\ApiControl;

class ReportController extends ApiControl
{
    public $layout = 'exercise';
    public $enableCsrfValidation = false;

    /**
     * 列表
     * @Author: yoyo
     * @create: 2023/3/9 15:03
     */
    public function actionIndex()
    {
        $where = "1=1";
        $type = Yii::$app->request->get('type');
        $reportType = Yii::$app->request->get('reportType');
        $startTime = strtotime(Yii::$app->request->get('startTime'));
        $endTime = strtotime(Yii::$app->request->get('endTime'));
        if ($type) $where .= " and type=$type";
        if ($reportType) $where .= " and reportType=$reportType";
        if ($startTime) $where .= " and startTime>$startTime";
        if ($endTime) {
            $endTime += 86400;
            $where .= " and createTime<" . $endTime;
        }
        $count = Report::find()->select("id")->where($where)->count();
        $page = new Pagination(['totalCount' => $count, 'pageSize' => 20]);
        $open_class = Report::find()->asArray()->where($where)->offset($page->offset)->orderBy('id desc')->limit($page->limit)->all();
        foreach ($open_class as $k => $v) {
            $tem = ExerciseList::find()->select('pid,catName,name')->asArray()->where('content_id=' . $v['contentId'])->one();
//            $tem = ExerciseList::find()->select('catName,name')->asArray()->where('content_id=' . $tem['pid'])->one();
//            $tem=ExerciseListDetail::find()->from('x2_exercise_list_detail eld')->select('el.catName,el.name')->asArray()->leftJoin('x2_exercise_list el','eld.pid=el.content_id')->where('eld.content_id='.$v['contentId'])->one();
            $user = User::find()->asArray()->where('uid=' . $v['uid'])->one();
            $open_class[$k]['name'] = $tem['catName'] . ' ' . $tem['name'];
            $open_class[$k]['username'] = $user['userName'];
        }
        return $this->render('index', ['content' => $open_class, 'page' => $page]);
    }

    /**
     * 处理报错
     * @Author: yoyo
     * @create: 2023/3/15 16:47
     */
    public function actionHandleStatus()
    {
        $id = Yii::$app->request->get('id');
        $re = Report::updateAll(['status' => 1], "id = $id");
        if ($re) {
            die('<script>alert("修改成功");history.go(-1);</script>');
        }
    }

}
